package com.example.carconsumer.controller.hkf;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.carinterface.pojo.BookingApplication;
import com.example.carinterface.pojo.BusinessOrder;
import com.example.carinterface.service.hkf.BusinessOrderService;
import com.example.carinterface.utils.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@CrossOrigin
@Api(value = "修车代办订单接口", tags = "修车代办订单接口")
@RestController
@RequestMapping("/user")
public class BusinessOrderController {
    @Reference
    BusinessOrderService businessOrderService;

    @PostMapping("/insertBusinessOrder")
    @ApiOperation(value = "客户提交申请", responseReference = "客户提交申请（客户端提交订单）", httpMethod = "post", response = ResultJson.class)
    public ResultJson insertBusinessOrder(@RequestBody @ApiParam(name = "bookingApplication", value = " maintenanceMode int COMMENT '1 自费修车  2 保险修车',\n" +
            "  repairShop_id  int COMMENT '修理厂或检测场或4s店id',\n" +
            "  pickCar int COMMENT '是否接车 1是 -1 否',\n" +
            "  pickTime datetime comment '接车时间',\n" +
            "  pickAdress VARCHAR(255) COMMENT '接车地址',\n" +
            "  pickCoordinate VARCHAR(255) COMMENT '接车坐标',\n" +
            "  returnAdress VARCHAR(255) COMMENT '还车地址',\n" +
            "  returnCoordinate VARCHAR(255) COMMENT '还车坐标',\n" +
            "  ownerCard_id  int COMMENT '车主卡id',\n" +
            "  ownerName VARCHAR(32) COMMENT '车主姓名',\n" +
            "  license_Plate_Number VARCHAR(255) comment '车牌号码',\n" +
            "  remarks VARCHAR(255) COMMENT '客户备注',\n" +
            "  telPhone VARCHAR(255) COMMENT '联系电话',\n" +
            "  orderNo  VARCHAR(255) COMMENT '支付单号',\n" +
            "  payMethod int   COMMENT '1，微信 2，支付宝',\n" +
            "  orderState int COMMENT '1已支付 2 未支付',\n" +
            "  allMoney   DOUBLE COMMENT '总金额',\n" +
            "  payMoney    DOUBLE COMMENT '支付金额',\n" +
            "  pointPay   DOUBLE COMMENT '积分付款',\n" +
            "  coupon_id  INT COMMENT '优惠卷id',\n" +
            "  payableMoney DOUBLE COMMENT '应付金额'" +
            "\"orderClass(订单；类型 1，估损预约修理 2直接预约修理 3代办验车 4年检标 5新车上牌),", required = true)Map map){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            if(map.get("pickTime")!=null){
            Date pickTime = simpleDateFormat.parse(map.get("pickTime").toString());
            map.put("pickTime",pickTime);}
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BookingApplication bookingApplication=new BookingApplication();
        BusinessOrder businessOrder =new BusinessOrder();
        try {
            BeanUtils.populate(bookingApplication,map);
            BeanUtils.populate(businessOrder,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return   businessOrderService.insertBusinessOrder(bookingApplication,businessOrder);
    }

    @PostMapping("/selectMyBusinessOrderState")
    @ApiOperation(value = "查询我的订单状态", responseReference = "查询我的订单状态", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectMyBusinessOrder(@ApiParam(name = "businessOrder_id", value = "当前用户id（businessOrderId）", required = true) @RequestBody Map<String,Object> map)
    {
        return  businessOrderService.selectMyBusinessOrderState(map);

    }


    @PostMapping("/updateBusinessOrder")
    @ApiOperation(value = "支付我的订单", responseReference = "支付我的订单", httpMethod = "post", response = ResultJson.class)
    public ResultJson updateBusinessOrder(@ApiParam(name = "map", value = "优惠劵id（coupon_id）积分（pointPay）应付金额（payableMoney） 支付金额（payMoney）申请表id（bookingApplicationId） 订单id（businessOrderId）支付单号（orderNo）支付方式（payMethod） 支付状态 1为已支付 2为未支付（orderState）", required = true) @RequestBody Map<String,Object> map)
    {
        return  businessOrderService.updateBusinessOrder(map);

    }

    @PostMapping("/insertDriverComment")
    @ApiOperation(value = "评价订单", responseReference = "评价订单", httpMethod = "post", response = ResultJson.class)
    public ResultJson insertDriverComment(@ApiParam(name = "map", value = " 订单id（businessOrderId） 评论内容（remarks） 星星数（remarks）用户id（userId）支付金额（payMoney）", required = true) @RequestBody Map<String,Object> map)
    {
        return  businessOrderService.insertDriverComment(map);

    }

    @PostMapping("/selectFinalPrice")
    @ApiOperation(value = "查询尾款接口", responseReference = "查询尾款接口", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectFinalPrice(@ApiParam(name = "map", value = " 订单id（businessOrderId） ", required = true) @RequestBody Map<String,Object> map)
    {
        return  businessOrderService.selectFinalPrice(map);

    }

    @PostMapping("/payFinalPrice")
    @ApiOperation(value = "支付尾款接口", responseReference = "支付尾款接口", httpMethod = "post", response = ResultJson.class)
    public ResultJson payFinalPrice(@ApiParam(name = "map", value = " 订单id（businessOrderId） ", required = true) @RequestBody Map<String,Object> map)
    {
        return  businessOrderService.payFinalPrice(map);

    }


    @PostMapping("/cancelOrder")
    @ApiOperation(value = "取消订单接口", responseReference = "取消订单接口", httpMethod = "post", response = ResultJson.class)
    public ResultJson cancelOrder(@ApiParam(name = "map", value = " 订单id（businessOrderId） ", required = true) @RequestBody Map<String,Object> map)
    {
        return  businessOrderService.cancelOrder(map);

    }

    @PostMapping("/tuikuanOrder")
    @ApiOperation(value = "申请退款接口", responseReference = "申请退款接口", httpMethod = "post", response = ResultJson.class)
    public ResultJson tuikuanOrder(@ApiParam(name = "map", value = " 订单id（businessOrderId） ", required = true) @RequestBody Map<String,Object> map)
    {
        return  businessOrderService.tuikuanOrder(map);

    }

}
